package com.bodystm.temperature.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.mimosa.common.system.user.TUser;
import com.mimosa.common.system.user.UserContext;


/**
 * 绯荤粺鐨勮繃婊ゅ櫒绫�
 * 
 * ClassName: LoginFilter <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * date: 2015骞�鏈�鏃�涓嬪崍10:57:01 <br/>
 *
 * @author Administrator
 * @version
 * @since JDK 1.7
 */
public class LoginCheckFilter implements Filter {

	@Override
	public void destroy(){
		
	}
	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) arg0;
		String path = request.getServletPath();
		if (!StringUtils.startsWith(path, "/home/")
				) {
			TUser user = (TUser) request.getSession().getAttribute(
					UserContext.USER_ATTR);
			if (user == null) {
				HttpServletResponse response = (HttpServletResponse) arg1;
				response.sendRedirect(request.getContextPath()
						+ "/home/index.action");
				return;
			} else {
				
			}
		}
		chain.doFilter(arg0, arg1);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		WebApplicationContext context = WebApplicationContextUtils
				.getRequiredWebApplicationContext(arg0.getServletContext());

	}
}